package io.renren.modules.work.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.work.entity.SubjectEntity;
import io.renren.modules.work.service.SubjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;
import java.util.UUID;

/**
 * 科目表
 *
 * @author kylin
 * @email 1360315221@qq.com
 * @date 2019-03-10 17:26:15
 */
@RestController
@RequestMapping("work/subject")
public class SubjectController {
    @Autowired
    private SubjectService subjectService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("work:subject:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = subjectService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("work:subject:info")
    public R info(@PathVariable("id") Integer id){
        SubjectEntity subject = subjectService.getById(id);

        return R.ok().put("subject", subject);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("work:subject:save")
    public R save(@RequestBody SubjectEntity subject){
        String uuid = UUID.randomUUID().toString();
        subject.setSubjUuid(uuid);
        subjectService.save(subject);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("work:subject:update")
    public R update(@RequestBody SubjectEntity subject){
        ValidatorUtils.validateEntity(subject);
        subjectService.updateById(subject);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("work:subject:delete")
    public R delete(@RequestBody Integer[] ids){
        subjectService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
